/*
 * WapAuthenticationInterceptor.java
 * Copyright 2021 Qunhe Tech, all rights reserved.
 * Qunhe PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
 */
package cn.org.itool.web.interceptor;

import cn.hutool.core.util.StrUtil;
import cn.org.itool.web.util.RequestAttrUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 从登录授权信息中鉴定登录状态, 已登录的情况下会调用doLogin方法 在未登录的情况会下调用 doNoLogin方法
 * 业务方可以重写doLogin和doNologin 方法来实现自己的特殊逻辑
 *
 * 未登陆状态下拦截器会返回false，终止流程，适用于需要登录状态的路径进行登录检查
 */
public abstract class WapAuthenticationInterceptor implements HandlerInterceptor {


	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		// 未登录
		String userId = RequestAttrUtil.getUserIdFromRequest();
		if (StrUtil.isBlank(userId)) {
			doNoLogin(request,response);
			return false;
		}

		doLogin(request, response);
		return true;
	}

	/**
	 * 未登录处理 子类实现该方法来指定未登录处理方案 如返回错误码，或者重定向到登录页面
	 *
	 * @param request
	 * @param response
	 */
	protected abstract void doNoLogin(HttpServletRequest request, HttpServletResponse response) throws Exception;

	/**
	 * 无操作权限
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	protected abstract void doNoPermission(HttpServletRequest request, HttpServletResponse response) throws Exception;

	/**
	 * 已登录处理
	 *
	 * @param request
	 * @param response
	 */
	protected void doLogin(HttpServletRequest request, HttpServletResponse response) {
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

}
